#!/bin/bash

set -e

ROOT=${ROOT:-$(pwd)}

tests_dir=${ROOT}/shared/tests/perf

display_usage () {
    echo "Usage: $0 TYPE SERVICE"
}


# Check for quiet mode
if [ ! -z $QUIET ]; then
    export OUTPUT_FILE=$(mktemp)
    exec 5>&1 6>&2 1>$OUTPUT_FILE 2>&1
fi

cleanup () {
    CODE=$?
    if [ ! -z $QUIET ]; then
        if [ ! $CODE -eq 0 ]; then
            cat $OUTPUT_FILE >&5
        fi
        rm $OUTPUT_FILE
    fi
}
trap cleanup EXIT

# Performance tests
tests_perf () {
    # Skip tests on environment where we shouldn't run tests
    yq -r ' .'${ENVIRONMENT}'.flags["can-tests-e2e"] | if . == null then true else . end ' $ROOT/environments.yaml | grep -q true || {
        echo "The environment $ENVIRONMENT does not support tests. Skipping"
        exit 0
    }

    ECOM_ENVIRONMENT=${ENVIRONMENT} \
    locust -f $tests_dir/perf_happy_path.py \
        --headless -u 500 -r 50 --run-time 1h
}

tests_perf